package com.dailyblue.java.project.shoppingcart.controller;

import com.dailyblue.java.project.shoppingcart.service.ShoppingCartService;
import com.dailyblue.java.project.shoppingcart.util.JsonResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/cart")
@Slf4j
public class ShoppingCartController {
    @Resource
    private ShoppingCartService shoppingCartService;

    @GetMapping("/{sessionId}/{bookId}/{num}")
    public JsonResult add(@PathVariable("sessionId") String sessionId, @PathVariable("bookId") int bookId, @PathVariable("num") int num) {
        return shoppingCartService.addCart(sessionId, bookId, num);
    }

    @GetMapping("/{bookId}/{num}")
    public JsonResult add(@PathVariable("bookId") int bookId, @PathVariable("num") int num, HttpServletRequest request) {
        String token = request.getHeader("token");
        log.info("token:{}", token);
        return shoppingCartService.addCart(bookId, num, token);
    }

    @GetMapping("/update/1/{bookId}/{num}")
    public JsonResult update(@PathVariable("bookId") int bookId, @PathVariable("num") int num, HttpServletRequest request) {
        return shoppingCartService.updateCart(bookId, num, request.getHeader("token"));
    }

    @GetMapping("/pay/1/2/3/4")
    public void pay(HttpServletRequest request) {
        String token = request.getHeader("token");
        log.info("token:{}", token);
        shoppingCartService.pay(token);
    }

    @GetMapping("/{token}")
    public JsonResult show(@PathVariable("token") String token) {
        return shoppingCartService.showCart(token);
    }
}
